package com.cgsd.heyu.widgets;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.library.adapter.ClickItemListener;
import com.cgsd.heyu.R;
import com.cgsd.heyu.adapter.CustomHomeTabAdapter;
import com.cgsd.heyu.entity.TabEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xiaoxing
 * Description 首页tab导航栏
 * CreateData: 2020/11/3
 */
@Deprecated
public class CustomHomeTab extends RelativeLayout implements ClickItemListener {
    /**
     * 首页
     */
    public static final String BRAND = "brand";
    /**
     * 任务
     */
    public static final String TASK = "task";
    /**
     * 我的
     */
    public static final String CENTER = "center";
    /**
     * 发现
     */
    public static final String FIND = "find";
    private Context context;
    private List<TabEntity> data = new ArrayList<>();
    private CustomHomeTabAdapter adapter;
    private HomeTabListener homeTabListener;

    public void setHomeTabListener(HomeTabListener homeTabListener) {
        this.homeTabListener = homeTabListener;
    }

    public CustomHomeTab(Context context) {
        super(context);
        this.context = context;
        init();
    }

    public CustomHomeTab(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    public CustomHomeTab(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
    }

    private void init() {
        initData();
        LayoutInflater.from(context).inflate(R.layout.layout_home_tab, this);
        RecyclerView mRecycler = findViewById(R.id.mRecyclerView);
        mRecycler.setLayoutManager(new GridLayoutManager(context, data.size()));
        adapter = new CustomHomeTabAdapter(context, data);
        mRecycler.setAdapter(adapter);

        adapter.setClickItemListener(this);
    }

    private void initData() {
        data.add(new TabEntity(context.getString(R.string.find), R.drawable.selector_tab_find, FIND));
        data.add(new TabEntity(context.getString(R.string.video),R.drawable.selector_tab_video, BRAND));
        data.add(new TabEntity(context.getString(R.string.task),R.drawable.selector_tab_task, TASK));
        data.add(new TabEntity(context.getString(R.string.center), R.drawable.selector_tab_my,CENTER));
    }

    @Override
    public void clickItem(View view, int position) {
        if (adapter.getSelectPosition() == position) {
            return;
        }
        adapter.setSelectPosition(position);
        adapter.notifyDataSetChanged();
        if (homeTabListener != null) {
            homeTabListener.clickTabPosition(position, data.get(position));
        }
    }

    /**
     * 设置选中位置
     */
    public void setSelectPosition(int position) {
        if (adapter.getSelectPosition() == position) {
            return;
        }
        adapter.setSelectPosition(position);
        adapter.notifyDataSetChanged();
    }

    public interface HomeTabListener {
        /**
         * 点击item数据回调
         *
         * @param position  点击位置
         * @param tabEntity 点击数据
         */
        void clickTabPosition(int position, TabEntity tabEntity);
    }
}
